<template>
    <!-- panel区域 -->
    <panel-head :route="route" />
    <!-- 搜索框区域 -->
    <div class="form">
        <el-form :inline="true" :model="searchForm">
            <el-form-item prop="out_trade_no">
                <el-input v-model="searchForm.out_trade_no" placeholder="订单号"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
        </el-form>
    </div>
    <!-- table区域 -->
    <el-table :data="tableData.list">
        <el-table-column prop="out_trade_no" label="订单号" width="150px" fixed="left" />
        <el-table-column prop="hospital_name" label="就诊医院" />
        <el-table-column prop="service_name" label="陪诊服务" />
        <el-table-column label="陪护师头像">
            <template #default="scope">
                <el-image :src="scope.row.companion.avatar" style="width: 50px; height: 50px;" />
            </template>
        </el-table-column>
        <el-table-column prop="companion.mobile" label="陪护师手机号" width="120px" />
        <el-table-column prop="price" label="总价" />
        <el-table-column prop="paidPrice" label="已付" />
        <el-table-column prop="order_start_time" label="下单时间">
            <template #default="scope">
                {{ dayjs(scope.row.order_start_time).format('YYYY-MM-DD') }}
            </template>
        </el-table-column>
        <el-table-column prop="trade_state" label="订单状态">
            <template #default="scope">
                <el-tag :type="stateMap(scope.row.trade_state)">{{ scope.row.trade_state }}</el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="service_state" label="接单状态" />
        <el-table-column prop="tel" label="联系人手机号" width="120px" />
        <el-table-column label="操作" fixed="right">
            <template #default="scope">
                <el-popconfirm
                    v-if="scope.row.trade_state === '待服务'"
                    :icon="InfoFilled"
                    icon-color="#626AEF"
                    title="是否确认服务完成？"
                    confirm-button-text="是"
                    cancel-button-text="否"
                    @confirm="confirmEvent(scope.row.out_trade_no)"
                >
                    <template #reference>
                        <el-button type="primary" link>服务完成</el-button>
                    </template>
                </el-popconfirm>
                <el-button v-else type="primary" link disabled>暂无服务</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页数据 -->
    <div class="pagination-info">
        <el-pagination
            v-model:current-page="pagination.pageNum"
            :page-size="pagination.pageSize"
            :background="false"
            size="small"
            layout="total, prev, pager, next"
            :total="tableData.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { InfoFilled } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'
import { getOrderList, updateOrder } from '@/api'
import dayjs from 'dayjs'

const route = useRoute()
// 分页数据
const pagination = reactive({
    pageNum: 1,
    pageSize: 10
})
// 表格数据
const tableData = reactive({
    list: [],
    total: 0
})
// 搜索表单
const searchForm = reactive({
    out_trade_no: ''
}) 
// 查询订单号
const onSubmit = () => {
    getTableData(searchForm)
}
// 调用接口，获取表格数据
const getTableData = (params = {}) => {
    getOrderList({ ...pagination, ...params }).then(({ data }) => {
        console.log(data)
        tableData.list = data.data.list
        tableData.total = data.data.total
    })
}
// 页面挂载之后，获取table数据
onMounted(() => {
    getTableData()
})
// 分页相关
const handleSizeChange = (val) => {
    pagination.pageSize = val
    getTableData()
}
const handleCurrentChange = (val) => {
    pagination.pageNum = val
    getTableData()
}

// 订单状态，映射
const stateMap = (key) => {
    const stateObj = {
        '已取消': 'info',
        '待支付': 'warning',
        '已完成': 'success',
        '待服务': 'primary'
    }
    return stateObj[key]
}
// 服务完成点击事件
const confirmEvent = (id) => {
    updateOrder({ id }).then(({ data }) => {
        if (data.code === 10000) {
            getTableData()
        }
    })
}

</script>

<style lang="less" scoped>
.form {
    display: flex;
    justify-content: flex-end;
    padding: 10px 0 10px 10px;
    background-color: #fff;
}
</style>